package edu.gatech.oad.antlab.person;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JTextPane;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 * Create a frame that holds the panels for our GUI. The panels hold the text boxes, radio buttons, and text fields. This will allow the player to configure their 
 * loading screen. 
 * @author Browntourage 
 *
 */
public class StartScreen extends JFrame
{
	//Declare all the text fields, radiobuttons, textpanes, player control and profession class. 
	private JPanel contentPane;
	private JTextField txtEnterNameHere;
	private JTextField txtPartyMember;
	private JTextField txtPartyMember_1;
	private JTextField txtPartyMember_2;
	private JTextField txtPartyMember_3;
	private final ButtonGroup profession = new ButtonGroup();
	private JRadioButton rdbtnSlow;
	private JRadioButton rdbtnMedium;
	private JRadioButton rdbtnFast;
	private final ButtonGroup pace = new ButtonGroup();
	private JRadioButton radioButton;
	private JRadioButton radioButton_1;
	private JRadioButton radioButton_2;
	private final ButtonGroup meals = new ButtonGroup();
	private JTextPane textPane;
	private JTextPane textPane_1;
	private JTextPane textPane_2;
	private JTextPane textPane_3;
	private JTextPane textPane_4;
	private JTextPane txtpnWelcomeToOregon;
	private JTextPane txtpnBrowntourage;
	private JRadioButton rdbtnCarpenter; 
	private JRadioButton rdbtnFarmer; 
	private JRadioButton rdbtnBanker;
	private final JButton btnStartGame;
	protected static PlayerControl pControl; 
	private Profession prof; 
	

	/**
	 * Launch the application.
	 * Create a main so that the panel can be launched on the frame. 
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					StartScreen frame = new StartScreen();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}


	/**
	 * Create the frame.
	 */
	public StartScreen()
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 531, 548);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		ButtonListener listener = new ButtonListener(); 
		
		
		txtpnWelcomeToOregon = new JTextPane();
		txtpnWelcomeToOregon.setEditable(false);
		txtpnWelcomeToOregon.setText("Welcome to Oregon Trail");
		GridBagConstraints gbc_txtpnWelcomeToOregon = new GridBagConstraints();
		gbc_txtpnWelcomeToOregon.insets = new Insets(0, 0, 5, 5);
		gbc_txtpnWelcomeToOregon.fill = GridBagConstraints.BOTH;
		gbc_txtpnWelcomeToOregon.gridx = 6;
		gbc_txtpnWelcomeToOregon.gridy = 0;
		contentPane.add(txtpnWelcomeToOregon, gbc_txtpnWelcomeToOregon);
		
		textPane_3 = new JTextPane();
		textPane_3.setEditable(false);
		textPane_3.setText("Name:");
		GridBagConstraints gbc_textPane_3 = new GridBagConstraints();
		gbc_textPane_3.gridwidth = 2;
		gbc_textPane_3.insets = new Insets(0, 0, 5, 5);
		gbc_textPane_3.fill = GridBagConstraints.BOTH;
		gbc_textPane_3.gridx = 3;
		gbc_textPane_3.gridy = 2;
		contentPane.add(textPane_3, gbc_textPane_3);
		
		txtEnterNameHere = new JTextField();
		txtEnterNameHere.setText("Enter name here");
		GridBagConstraints gbc_txtEnterNameHere = new GridBagConstraints();
		gbc_txtEnterNameHere.insets = new Insets(0, 0, 5, 5);
		gbc_txtEnterNameHere.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtEnterNameHere.gridx = 6;
		gbc_txtEnterNameHere.gridy = 2;
		contentPane.add(txtEnterNameHere, gbc_txtEnterNameHere);
		txtEnterNameHere.setColumns(10);
		
		textPane_2 = new JTextPane();
		textPane_2.setEditable(false);
		textPane_2.setText("Party Members:");
		GridBagConstraints gbc_textPane_2 = new GridBagConstraints();
		gbc_textPane_2.gridwidth = 3;
		gbc_textPane_2.insets = new Insets(0, 0, 5, 5);
		gbc_textPane_2.fill = GridBagConstraints.BOTH;
		gbc_textPane_2.gridx = 3;
		gbc_textPane_2.gridy = 3;
		contentPane.add(textPane_2, gbc_textPane_2);
		
		txtPartyMember = new JTextField();
		txtPartyMember.setText("Party Member 1");
		GridBagConstraints gbc_txtPartyMember = new GridBagConstraints();
		gbc_txtPartyMember.insets = new Insets(0, 0, 5, 5);
		gbc_txtPartyMember.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtPartyMember.gridx = 6;
		gbc_txtPartyMember.gridy = 3;
		contentPane.add(txtPartyMember, gbc_txtPartyMember);
		txtPartyMember.setColumns(10);
		
		txtPartyMember_1 = new JTextField();
		txtPartyMember_1.setText("Party Member 2");
		GridBagConstraints gbc_txtPartyMember_1 = new GridBagConstraints();
		gbc_txtPartyMember_1.insets = new Insets(0, 0, 5, 5);
		gbc_txtPartyMember_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtPartyMember_1.gridx = 6;
		gbc_txtPartyMember_1.gridy = 4;
		contentPane.add(txtPartyMember_1, gbc_txtPartyMember_1);
		txtPartyMember_1.setColumns(10);
		
		txtPartyMember_2 = new JTextField();
		txtPartyMember_2.setText("Party Member 3");
		GridBagConstraints gbc_txtPartyMember_2 = new GridBagConstraints();
		gbc_txtPartyMember_2.insets = new Insets(0, 0, 5, 5);
		gbc_txtPartyMember_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtPartyMember_2.gridx = 6;
		gbc_txtPartyMember_2.gridy = 5;
		contentPane.add(txtPartyMember_2, gbc_txtPartyMember_2);
		txtPartyMember_2.setColumns(10);
		
		txtPartyMember_3 = new JTextField();
		txtPartyMember_3.setText("Party Member 4");
		GridBagConstraints gbc_txtPartyMember_3 = new GridBagConstraints();
		gbc_txtPartyMember_3.insets = new Insets(0, 0, 5, 5);
		gbc_txtPartyMember_3.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtPartyMember_3.gridx = 6;
		gbc_txtPartyMember_3.gridy = 6;
		contentPane.add(txtPartyMember_3, gbc_txtPartyMember_3);
		txtPartyMember_3.setColumns(10);
		
		textPane = new JTextPane();
		textPane.setEditable(false);
		textPane.setText("Profession:");
		GridBagConstraints gbc_textPane = new GridBagConstraints();
		gbc_textPane.gridwidth = 3;
		gbc_textPane.insets = new Insets(0, 0, 5, 5);
		gbc_textPane.fill = GridBagConstraints.BOTH;
		gbc_textPane.gridx = 3;
		gbc_textPane.gridy = 7;
		contentPane.add(textPane, gbc_textPane);
		
		rdbtnCarpenter = new JRadioButton("Carpenter");
		rdbtnCarpenter.addActionListener(listener); 
		profession.add(rdbtnCarpenter);
		GridBagConstraints gbc_rdbtnCarpenter = new GridBagConstraints();
		gbc_rdbtnCarpenter.anchor = GridBagConstraints.WEST;
		gbc_rdbtnCarpenter.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnCarpenter.gridx = 6;
		gbc_rdbtnCarpenter.gridy = 7;
		contentPane.add(rdbtnCarpenter, gbc_rdbtnCarpenter);
		
		rdbtnBanker = new JRadioButton("Banker");
		rdbtnBanker.addActionListener(listener); 
		profession.add(rdbtnBanker);
		GridBagConstraints gbc_rdbtnBanker = new GridBagConstraints();
		gbc_rdbtnBanker.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnBanker.anchor = GridBagConstraints.WEST;
		gbc_rdbtnBanker.gridx = 6;
		gbc_rdbtnBanker.gridy = 8;
		contentPane.add(rdbtnBanker, gbc_rdbtnBanker);
		
		rdbtnFarmer = new JRadioButton("Farmer");
		rdbtnFarmer.addActionListener(listener); 
		profession.add(rdbtnFarmer);
		GridBagConstraints gbc_rdbtnFarmer = new GridBagConstraints();
		gbc_rdbtnFarmer.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnFarmer.anchor = GridBagConstraints.WEST;
		gbc_rdbtnFarmer.gridx = 6;
		gbc_rdbtnFarmer.gridy = 9;
		contentPane.add(rdbtnFarmer, gbc_rdbtnFarmer);
		
		textPane_4 = new JTextPane();
		textPane_4.setEditable(false);
		textPane_4.setText("Pace:");
		GridBagConstraints gbc_textPane_4 = new GridBagConstraints();
		gbc_textPane_4.gridwidth = 2;
		gbc_textPane_4.insets = new Insets(0, 0, 5, 5);
		gbc_textPane_4.fill = GridBagConstraints.BOTH;
		gbc_textPane_4.gridx = 3;
		gbc_textPane_4.gridy = 10;
		contentPane.add(textPane_4, gbc_textPane_4);
		
		rdbtnSlow = new JRadioButton("Slow");
		pace.add(rdbtnSlow);
		rdbtnSlow.addActionListener(listener); 
		GridBagConstraints gbc_rdbtnSlow = new GridBagConstraints();
		gbc_rdbtnSlow.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnSlow.anchor = GridBagConstraints.WEST;
		gbc_rdbtnSlow.gridx = 6;
		gbc_rdbtnSlow.gridy = 10;
		contentPane.add(rdbtnSlow, gbc_rdbtnSlow);
		
		rdbtnMedium = new JRadioButton("Medium");
		
		pace.add(rdbtnMedium);
		rdbtnMedium.addActionListener(listener); 
		GridBagConstraints gbc_rdbtnMedium = new GridBagConstraints();
		gbc_rdbtnMedium.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnMedium.anchor = GridBagConstraints.WEST;
		gbc_rdbtnMedium.gridx = 6;
		gbc_rdbtnMedium.gridy = 11;
		contentPane.add(rdbtnMedium, gbc_rdbtnMedium);
		
		rdbtnFast = new JRadioButton("Fast");
		pace.add(rdbtnFast);
		rdbtnFast.addActionListener(listener); 
		GridBagConstraints gbc_rdbtnFast = new GridBagConstraints();
		gbc_rdbtnFast.insets = new Insets(0, 0, 5, 5);
		gbc_rdbtnFast.anchor = GridBagConstraints.WEST;
		gbc_rdbtnFast.gridx = 6;
		gbc_rdbtnFast.gridy = 12;
		contentPane.add(rdbtnFast, gbc_rdbtnFast);
		
		textPane_1 = new JTextPane();
		textPane_1.setEditable(false);
		textPane_1.setText("Meals per day:");
		GridBagConstraints gbc_textPane_1 = new GridBagConstraints();
		gbc_textPane_1.gridwidth = 3;
		gbc_textPane_1.insets = new Insets(0, 0, 5, 5);
		gbc_textPane_1.fill = GridBagConstraints.BOTH;
		gbc_textPane_1.gridx = 3;
		gbc_textPane_1.gridy = 13;
		contentPane.add(textPane_1, gbc_textPane_1);
		
		radioButton = new JRadioButton("1");
		radioButton.addActionListener(listener); 
		meals.add(radioButton);
		GridBagConstraints gbc_radioButton = new GridBagConstraints();
		gbc_radioButton.insets = new Insets(0, 0, 5, 5);
		gbc_radioButton.anchor = GridBagConstraints.WEST;
		gbc_radioButton.gridx = 6;
		gbc_radioButton.gridy = 13;
		contentPane.add(radioButton, gbc_radioButton);
		
		radioButton_1 = new JRadioButton("2");
		meals.add(radioButton_1);
		radioButton_1.addActionListener(listener); 
		GridBagConstraints gbc_radioButton_1 = new GridBagConstraints();
		gbc_radioButton_1.insets = new Insets(0, 0, 5, 5);
		gbc_radioButton_1.anchor = GridBagConstraints.WEST;
		gbc_radioButton_1.gridx = 6;
		gbc_radioButton_1.gridy = 14;
		contentPane.add(radioButton_1, gbc_radioButton_1);
		
		radioButton_2 = new JRadioButton("3");
		meals.add(radioButton_2);
		radioButton_2.addActionListener(listener); 
		GridBagConstraints gbc_radioButton_2 = new GridBagConstraints();
		gbc_radioButton_2.insets = new Insets(0, 0, 5, 5);
		gbc_radioButton_2.anchor = GridBagConstraints.WEST;
		gbc_radioButton_2.gridx = 6;
		gbc_radioButton_2.gridy = 15;
		contentPane.add(radioButton_2, gbc_radioButton_2);
		
		btnStartGame = new JButton("START GAME");
		StartButtonListener listener2 = new StartButtonListener();
		btnStartGame.addActionListener(listener2);
		GridBagConstraints gbc_btnStartGame = new GridBagConstraints();
		gbc_btnStartGame.insets = new Insets(0, 0, 5, 5);
		gbc_btnStartGame.gridx = 6;
		gbc_btnStartGame.gridy = 16;
		contentPane.add(btnStartGame, gbc_btnStartGame);
		
		txtpnBrowntourage = new JTextPane();
		txtpnBrowntourage.setEditable(false);
		txtpnBrowntourage.setForeground(Color.BLACK);
		txtpnBrowntourage.setText("Browntourage");
		GridBagConstraints gbc_txtpnBrowntourage = new GridBagConstraints();
		gbc_txtpnBrowntourage.gridwidth = 3;
		gbc_txtpnBrowntourage.fill = GridBagConstraints.BOTH;
		gbc_txtpnBrowntourage.gridx = 13;
		gbc_txtpnBrowntourage.gridy = 17;
		contentPane.add(txtpnBrowntourage, gbc_txtpnBrowntourage);
		
	}
	/**
	 * Create a private class ButtonListener which will allow for the buttons to be listened to and be able to get the information entered in the GUI. 
	 * 
	 */
	private class ButtonListener implements ActionListener{
		
		public void actionPerformed(ActionEvent ae){
			String name = txtEnterNameHere.getText(); 
			String troop1 = txtPartyMember.getText(); 
			String troop2 = txtPartyMember_1.getText(); 
			String troop3 = txtPartyMember_2.getText(); 
			String troop4 = txtPartyMember_3.getText(); 
			
			if(ae.getActionCommand() == "Farmer"){
				prof = new Profession("Farmer"); 
				pControl = new PlayerControl(name, prof);
				System.out.println("Player Name: " + pControl.getpName()); 
				System.out.println("Profession: " + prof.getProfName()); 
				prof.setSalary(1000);
				pControl.setCash(prof.getSalary());
				
			}
			if(ae.getActionCommand() == "Carpenter"){
				prof = new Profession("Carpenter"); 
				pControl = new PlayerControl(name, prof); 
				System.out.println("Player Name: " + pControl.getpName()); 
				System.out.println("Profession: " + prof.getProfName()); 
				prof.setSalary(1500); 
				pControl.setCash(prof.getSalary());
			}
			if(ae.getActionCommand() == "Banker"){
				prof = new Profession("Banker"); 
				pControl = new PlayerControl(name, prof); 
				System.out.println("Player Name: " + pControl.getpName()); 
				System.out.println("Profession: " + prof.getProfName()); 
				prof.setSalary(2000);
				pControl.setCash(prof.getSalary());
			}
			pControl.pickTroops(troop1, troop2, troop3, troop4); 
			if(ae.getActionCommand() == "Slow"){
				pControl.setSpeed(50.00); 
				pControl.setPace(pControl.getSpeed());
				System.out.println("Pace: "+ pControl.getPace()); 

			}
			
			if(ae.getActionCommand() == "Medium"){
				pControl.setSpeed(100.00); 
				pControl.setPace(pControl.getSpeed()); 
				System.out.println("Pace: "+ pControl.getPace());
			}
			if(ae.getActionCommand() == "Fast"){
				pControl.setSpeed(150.00); 
				pControl.setPace(pControl.getSpeed()); 
				System.out.println("Pace: "+ pControl.getPace());
			}
			if(ae.getActionCommand() == "1"){
				pControl.setAmount(1); 
				pControl.setRations(pControl.getAmount());
				System.out.println("Rations: "+ pControl.getRations());
			}
			if(ae.getActionCommand() == "2"){
				pControl.setAmount(2); 
				pControl.setRations(pControl.getAmount()); 
				System.out.println("Rations: "+ pControl.getRations());
			}
			if(ae.getActionCommand() == "3"){
				pControl.setAmount(3);
				pControl.setRations(pControl.getAmount()); 
				System.out.println("Rations: "+ pControl.getRations());
			}
			System.out.println("Toop 1: " + troop1 + " Troop 2: " + troop2 + " Troop 3: " + troop3 + " Troop 4: " + troop4); 
					
		}
		
	}
	/**
	 * Create another private class that will give out JOptionPane when the user does not enter the information completely. 
	 *
	 */
	private class StartButtonListener implements ActionListener{
		
		public void actionPerformed(ActionEvent ae){
			String name = txtEnterNameHere.getText();
			if(name.equals("") || name.equals("Enter name here")){
				
				JOptionPane.showMessageDialog(null, "Please Enter a Name!!");
			}
			
			else if (profession.isSelected(null)){
				
				JOptionPane.showMessageDialog(null, "Please Select a Profession!!");
			}
			
			else if (pace.isSelected(null)){
							
							JOptionPane.showMessageDialog(null, "Please Select Your Pace!!");
						}
			else if (meals.isSelected(null)){
				
				JOptionPane.showMessageDialog(null, "Please Select Your Meals Per Day!!");
			}
			
			else{
				StoreScreen screen = new StoreScreen();
				screen.main(null);
			}
		}
		
	}

}
